package com.gzkemays.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
import org.springframework.security.oauth2.provider.code.InMemoryAuthorizationCodeServices;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;

import javax.annotation.Resource;

/**
 * @title 授权服务器配置
 * @author gzkemays
 * @date 2021/3/18 14:18
 */
@EnableAuthorizationServer
@Configuration
public class AuthorizationConfig extends AuthorizationServerConfigurerAdapter {
  @Resource TokenStore tokenStore;
  @Resource ClientDetailsService clientDetailsService;

  @Bean
  public AuthorizationServerTokenServices tokenServices() {
    DefaultTokenServices services = new DefaultTokenServices();
    services.setClientDetailsService(clientDetailsService);
    services.setTokenStore(tokenStore);
    services.setSupportRefreshToken(true);
    // token 验证时效 5 小时
    services.setAccessTokenValiditySeconds(60 * 60 * 5);
    // token 有效时间 1 天
    services.setRefreshTokenValiditySeconds(60 * 60 * 24);
    return services;
  }

  @Override
  public void configure(AuthorizationServerSecurityConfigurer security) {
    // 设置端点通行，校验 token 时会访问该端点。
    security.checkTokenAccess("permitAll()").allowFormAuthenticationForClients();
  }

  @Override
  public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    // 配置客户端详细信息
    clients
        .inMemory()
        .withClient("gzkemays")
        .secret(new BCryptPasswordEncoder().encode("gzkemays"))
        .resourceIds("res1")
        .authorizedGrantTypes("authorization_code", "refresh_token")
        .scopes("all")
        .redirectUris("http://localhost:5002/index.html");
  }

  @Override
  public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
    // 通过 codeServices 配置授权码存储，tokenService 令牌存储。
    // tokenService 可以用于配置 token 是否支持刷新、存储位置、有效期以及刷新 Token 有效期等。
    endpoints.authorizationCodeServices(authorizationCodeServices()).tokenServices(tokenServices());
  }

  @Bean
  public AuthorizationCodeServices authorizationCodeServices() {
    return new InMemoryAuthorizationCodeServices();
  }
}
